<?php

declare(strict_types=1);

namespace App\Helpers;

/**
 * 发送Util类
 * @author ShenWei
 *
 */
class SmsWelinkUtil
{

    public $SMmsEncryptKey = 'SMmsEncryptKey';
    public $corpId = '';
    public $productId = '1012888';
    public $accountId = 'dlszxylq';
    public $password = 'xinyoulinqi123';

    public function __construct()
    {
    }

    public function send($phoneNumbers = [], $msg = '')
    {
        if (!$phoneNumbers) return false;
        if (!is_array($phoneNumbers)) $phoneNumbers = [$phoneNumbers];
        $random = $this->getRandom();
        $curTime = time();
        $wholeUrl = 'http://api.51welink.com/EncryptionSubmit/SendSms.ashx';
        $data = new \stdClass();
        $data->AccountId = $this->accountId;
        $data->Timestamp = $curTime;
        $data->ExtendNo = $this->corpId;
        $data->Random = $random;
        $data->ProductId = $this->productId;
        $data->PhoneNos = join(',', $phoneNumbers);
        $data->Content = $msg;
        $data->SendTime = '';
        $data->OutId = '';
        $data->AccessKey = $this->calculateSignSender($this->accountId, $this->password, $random, $curTime, $phoneNumbers[0]);
        // var_dump($data);
        // return $this->sendCurlPost($wholeUrl, $data);
        return Helper::httpReq($wholeUrl, $data, 'json');
    }

    /**
     * 生成随机数
     * @return number
     */
    public function getRandom()
    {
        return rand(100000, 999999);
    }

    function getPassword($password)
    {
        return strtoupper(md5($password . $this->SMmsEncryptKey));
    }

    /**
     * 生成签名
     * @param string 账号
     * @param string 密码
     * @param long 随机数
     * @param long Linux 时间戳
     * @return string 授权结果
     */
    public function calculateSignPuller($account, $password, $random, $time)
    {
        return hash('sha256', 'AccountId=' . $account . '&Password=' . $this->getPassword($password)
            . '&Random=' . $random . '&Timestamp=' . $time);
    }

    /**
     * 生成签名
     * @param string 账号
     * @param string 密码
     * @param long 随机数
     * @param long Linux 时间戳
     * @param long 手机号码
     * @return string 授权结果
     */
    public function calculateSignSender($account, $password, $random, $time, $phoneNumber)
    {
        return hash('sha256', 'AccountId=' . $account . '&PhoneNos=' . $phoneNumber . '&Password=' . $this->getPassword($password)
            . '&Random=' . $random . '&Timestamp=' . $time);
    }

    /**
     * 生成签名
     * @param string $account 账号
     * @param string $password 密码
     * @param long $random 随机数
     * @param long $time 时间戳
     * @return string 授权结果
     */
    public function calculateSignIndividuationSender($account, $password, $random, $time)
    {
        return hash('sha256', 'AccountId=' . $account . '&Password=' . $this->getPassword($password)
            . '&Random=' . $random . '&Timestamp=' . $time);
    }
    /**
     * @param string $account 账号
     * @param string $password 密码
     * @param long $random 随机数
     * @param long $time 时间戳
     * @param long 手机号码
     * @param int 模板Id
     * @return string 授权结果
     */
    public function calculateSignTemplateSender($account, $password, $random, $time, $phoneNumber, $tempCode)
    {
        return hash('sha256', 'AccountId=' . $account . '&PhoneNos=' . $phoneNumber . '&Password=' . $this->getPassword($password)
            . '&Random=' . $random . '&TempCode=' . $tempCode . '&Timestamp=' . $time);
    }

    /**
     * 发送请求
     * @param string $url 请求地址
     * @param array $dataObj 请求内容
     * @return string 应答Json字符串
     */
    public function sendCurlPost($url, $dataObj)
    {
        $headers = array(
            'Content-type: application/json;charset="utf-8"'
        );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        $ret = curl_exec($curl);
        if (false == $ret) {
            $result = '{ "State":' . -2 . ',"MsgState":"' . curl_error($curl) . '"}';
        } else {
            $rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if (200 != $rsp) {
                $result = '{ \"State\":' . -1 . ',"MsgState":"' . $rsp . ' ' . curl_error($curl) . '"}';
            } else {
                $result = $ret;
            }
        }
        curl_close($curl);

        return $result;
    }
}
